extends Node

#region  Steam初始化
func _init_steam() -> void:
	# TODO - 正式发布时取消注释
	# if Steam.restartAppIfNecessary(GameData.APP_ID):
	# 	get_tree().quit()
	# 	return
	var initialize_response: Dictionary = Steam.steamInitEx(GameData.APP_ID, true)
	if initialize_response["status"] == 0:
		GameData.steam_id = Steam.getSteamID()
		GameData.steam_name = Steam.getPersonaName()


# TODO - 获取头像
#endregiont available - running in non-Steam context")


func _ready() -> void:
	_init_steam()


func set_puzzle_solved(solved: bool, key: String) -> void:
	GameData.puzzle_solved[key] = solved


func back_to_main_menu() -> void:
	get_tree().change_scene_to_file("res://main.tscn")


func save_game() -> void:
	var save_data: Dictionary = {
									"puzzle_unlocked": GameData.puzzle_unlocked,
									"puzzle_solved": GameData.puzzle_solved
								}

	var file: FileAccess = FileAccess.open(GameData.SAVE_FILE, FileAccess.WRITE)
	if file:
		file.store_line(JSON.stringify(save_data))
		file.close()


func load_game() -> void:
	if not FileAccess.file_exists(GameData.SAVE_FILE):
		return

	var file: FileAccess = FileAccess.open(GameData.SAVE_FILE, FileAccess.READ)
	if file:
		var data = JSON.parse_string(file.get_line())
		if data:
			if data.has("puzzle_unlocked"):
				GameData.puzzle_unlocked = data["puzzle_unlocked"]
			if data.has("puzzle_solved"):
				GameData.puzzle_solved = data["puzzle_solved"]
		file.close()
		
